/**
* @project admin-go-api
* @name sysOperationLog
* @descriptions 操作日志数据层
* @author 寒锋
* @date 2023-12-22 16:05:57
 */

package dao

import (
	"admin-go-api/api/entity"
	. "admin-go-api/pkg/db"
	"fmt"
)

// CreateSysOperationLog 添加操作日志
func CreateSysOperationLog(log entity.SysOperationLog) bool {
	result := Db.Create(&log)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}

// GetSysOperationLogList 操作日志列表
func GetSysOperationLogList(PageNum, PageSize int, Username, BeginTime, EndTime string) (sysOperationLog []entity.SysOperationLog, count int64) {
	model := Db.Table("sys_operation_log")
	if Username != "" {
		model = model.Where("username = ?", Username)
	}
	if BeginTime != "" && EndTime != "" {
		model = model.Where("create_time between ? and ?", BeginTime, EndTime)
	}
	model.Count(&count)
	model.Limit(PageSize).Offset((PageNum - 1) * PageSize).Order("create_time").Find(&sysOperationLog)
	return sysOperationLog, count
}

// DeleteSysOperationLogById 根据id删除日志
func DeleteSysOperationLogById(dto entity.SysOperationLogIdDto) bool {
	result := Db.Delete(&entity.SysOperationLog{}, dto.Id)
	if result.RowsAffected > 0 {
		return true
	}
	return false
}

// BatchDeleteSysOperationLog 批量删除日志
func BatchDeleteSysOperationLog(dto entity.BatchDeleteSysOperationLogDto) bool {
	result := Db.Where("id in (?)", dto.Ids).Delete(&entity.SysOperationLog{})
	if result.RowsAffected > 0 {
		return true
	}
	return false
}

// CleanSysOperationLog 清空日志
func CleanSysOperationLog() bool {
	result := Db.Exec("truncate table sys_operation_log")
	fmt.Println(result.Error)
	if result.Error == nil {
		return true
	}
	return false
}
